"""
@FileName：main_window_ui.py\n
@Description：\n
@Author：锦沐Python\n
@Time：2024/8/16 19:08\n
"""
import tkinter as tk
from tkinter import ttk

from ttkbootstrap import Style
from ReLearning.src.config import *

class MainWindowUi():
    """
    主窗口
    """

    def __init__(self):
        # 创建一个窗口，配置他的基础信息
        self.root = tk.Tk()
        self.root.geometry(f"{WIN_WIDTH}x{WIN_HEIGHT}+5+5")
        self.root.minsize(WIN_WIDTH, WIN_HEIGHT)
        self.root.title(WIN_NAME)
        # 设置基础参数
        self.root.iconbitmap(ICON_PATH)
        # 主题样式
        Style(THEME_STYLE)
        # 变量
        # 正则表达式
        self.expression_entry_data = tk.StringVar(value="PASSLINK团队，小红书：锦沐python")
        # 模式选项
        self.select_modes_state = [tk.IntVar(value=0) for _ in range(len(MODE))]
        # 替换值
        self.replace_entry_data = tk.StringVar(value="")
        # 菜单按钮组
        self.menu_buttons = []
        # 布局容器
        self.create_container()
        # 填充容器
        self.fill_container()

    def run_app(self):
        """
        启动主循环
        """
        self.root.mainloop()

    def create_container(self):
        """
        创建容器
        """
        # 根容器
        self.root_container_frame = ttk.Frame(self.root)
        self.root_container_frame.pack(expand=True, fill=tk.BOTH, pady=5, padx=5)

        # 顶部输入框
        self.regular_expression_frame = ttk.Frame(self.root_container_frame)
        self.regular_expression_frame.pack(side=tk.TOP, expand=True, fill=tk.X)

        # 文本框容器
        self.text_container_frame = ttk.Frame(self.root_container_frame)
        self.text_container_frame.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

        # 菜单栏
        self.menu_frame = ttk.Frame(self.text_container_frame)
        self.menu_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        # 中心文本框
        self.center_text_frame = ttk.Frame(self.text_container_frame)
        self.center_text_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        # 右侧提示
        self.tips_frame = ttk.Frame(self.text_container_frame)
        self.tips_frame.pack(side=tk.RIGHT, expand=False, fill=tk.Y)

    def fill_container(self):
        """
        填充容器
        """
        # 表达式输入框
        self.expression_entry = ttk.Entry(self.regular_expression_frame, justify=tk.CENTER,
                                          textvariable=self.expression_entry_data)
        self.expression_entry.pack(expand=True, fill=tk.X, pady=5, padx=5)

        # 匹配模式选择
        # 编码集二选一
        self.au_mode = tk.IntVar(value=0)
        self.UNICODE_mode_redio = ttk.Radiobutton(self.regular_expression_frame,
                                                  text=ENCODING_MODE_NAME[0],
                                                  variable=self.au_mode, value=0)
        self.ASCII_mode_redio = ttk.Radiobutton(self.regular_expression_frame,
                                                text=ENCODING_MODE_NAME[1],
                                                variable=self.au_mode, value=1)

        self.UNICODE_mode_redio.pack(side=tk.RIGHT, padx=5)
        self.ASCII_mode_redio.pack(side=tk.RIGHT, padx=5)

        # 匹配选项多选
        option_vars = list(zip(self.select_modes_state, MODE_NAME))
        for value, option in option_vars:
            # 创建并放置Checkbutton选项
            ttk.Checkbutton(self.regular_expression_frame,
                            text=option, variable=value, onvalue=1,
                            offvalue=0, padding=3).pack(side=tk.LEFT, padx=5)

        # 菜单按钮
        for item in MEAU_ITEMS:
            button = ttk.Button(self.menu_frame, text=item)
            button.pack(pady=3, padx=3, expand=False, fill=tk.X)
            self.menu_buttons.append(button)

        # 中间测试文本框
        self.data_text = tk.Text(self.center_text_frame)
        self.data_text.pack(pady=5, padx=3, expand=True, fill=tk.BOTH)

        # 创建底部切换窗口
        self.notebook = ttk.Notebook(self.center_text_frame)

        # 字符串替换界面
        self.notebook_page1 = ttk.Frame(self.notebook)
        # 替换字符串内容
        self.replace_entry = ttk.Entry(self.notebook_page1, textvariable=self.replace_entry_data)
        self.replace_entry.pack(pady=5, padx=3, expand=True, fill=tk.X)
        # 替换结果
        self.replace_result_txt = tk.Text(self.notebook_page1)
        self.replace_result_txt.pack(pady=5, padx=3, expand=True, fill=tk.BOTH)

        # 表达式参考列表界面
        self.notebook_page2 = ttk.Frame(self.notebook)
        self.expression_title_list = ttk.Treeview(self.notebook_page2, columns=[ExpressionsDetailTitle])
        # 绑定 Treeview 点击事件
        # 为操作列配置可选值
        self.expression_title_list.column("#0", width=0, anchor=tk.W, stretch=tk.NO)
        self.expression_title_list.column(ExpressionsDetailTitle, width=100, anchor=tk.CENTER)
        self.expression_title_list.heading(ExpressionsDetailTitle, text=ExpressionsDetailTitle)
        self.expression_title_list.pack(expand=False, fill=tk.BOTH, side=tk.LEFT)

        # 表达式项详情
        self.document_list = ttk.Treeview(self.notebook_page2, columns=ExpressionsDetailCloums)
        self.document_list.column("#0", width=0, stretch=tk.NO)  # 隐藏默认的第一列索引列
        self.document_list.column(ExpressionsDetailCloums[0], width=20, anchor=tk.W)
        self.document_list.column(ExpressionsDetailCloums[1], width=40, anchor=tk.CENTER)
        # 设置列标题
        self.document_list.heading(ExpressionsDetailCloums[0], text=ExpressionsDetailCloums[0])
        self.document_list.heading(ExpressionsDetailCloums[1], text=ExpressionsDetailCloums[1])
        self.document_list.pack(side=tk.RIGHT, expand=True, fill=tk.BOTH)

        # 学习模式
        self.notebook_page3 = ttk.Frame(self.notebook)
        # 表达式项详情
        self.rex_learn_list = ttk.Treeview(self.notebook_page3, columns=ExpressionsDetailCloums)
        self.rex_learn_list.column("#0", width=0, stretch=tk.NO)  # 隐藏默认的第一列索引列
        self.rex_learn_list.column(ExpressionsDetailCloums[0], width=20, anchor=tk.W)
        self.rex_learn_list.column(ExpressionsDetailCloums[1], width=40, anchor=tk.CENTER)
        # 设置列标题
        self.rex_learn_list.heading(ExpressionsDetailCloums[0], text=ExpressionsDetailCloums[0])
        self.rex_learn_list.heading(ExpressionsDetailCloums[1], text=ExpressionsDetailCloums[1])
        self.rex_learn_list.pack(side=tk.RIGHT, expand=True, fill=tk.BOTH)

        # 将页面添加到 Notebook
        self.notebook.add(self.notebook_page1, text=NOTE_BOOK_PAGE[0])
        self.notebook.add(self.notebook_page2, text=NOTE_BOOK_PAGE[1])
        self.notebook.add(self.notebook_page3, text=NOTE_BOOK_PAGE[2])

        self.notebook.pack(padx=3, side=tk.BOTTOM, fill=tk.BOTH, expand=True)

        # 右边提示文本框
        self.tips_text = tk.Text(self.tips_frame)
        self.tips_text.pack(side=tk.TOP, pady=5, padx=3, expand=True, fill=tk.BOTH)

        # 右边匹配项详情
        self.match_list = ttk.Treeview(self.tips_frame, columns=TestExpressDetailCloum)

        # 配置列属性
        self.match_list.column("#0", width=0, stretch=tk.NO)
        self.match_list.column(TestExpressDetailCloum[0], width=40, anchor=tk.CENTER)
        self.match_list.column(TestExpressDetailCloum[1], width=70, anchor=tk.CENTER)
        self.match_list.column(TestExpressDetailCloum[2], width=150, anchor=tk.CENTER)

        # 设置列标题
        self.match_list.heading(TestExpressDetailCloum[0], text=TestExpressDetailCloum[0])
        self.match_list.heading(TestExpressDetailCloum[1], text=TestExpressDetailCloum[1])
        self.match_list.heading(TestExpressDetailCloum[2], text=TestExpressDetailCloum[2])
        # 布局 Treeview
        self.match_list.pack(side=tk.BOTTOM, pady=5, padx=3, expand=True, fill=tk.BOTH)

    def show_notebook(self):
        """
        显示 notebook
        """
        self.notebook.pack(padx=3, side=tk.BOTTOM, fill=tk.BOTH, expand=True)

    def hide_notebook(self):
        """
        隐藏 notebook
        """
        self.notebook.pack_forget()
